package cloud.maque.auth.common;

import cloud.maque.auth.entity.AuthUser;
import cloud.maque.auth.entity.AuthUserThrid;
import cloud.maque.auth.service.UserService;
import cloud.maque.auth.service.UserThridService;
import cloud.maque.common.core.enums.LoginType;
import cloud.maque.common.core.exceptions.BusinessException;
import cloud.maque.sso.client.model.ThridSite;
import cloud.maque.sso.client.model.UserModel;
import cloud.maque.sso.client.service.SsoClientAuthUserService;
import cn.hutool.core.util.IdUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 实现sso-client调用的接口
 */
@Service
@Primary //一定要加这个注解，否则会报错
public class SsoClientAuthUserServiceImpl implements SsoClientAuthUserService {

    @Autowired
    UserService userService;


    @Autowired
    UserThridService userThridService;

    @Override
    public String getUserIdByOpenid(String openid) {
        AuthUser user = userService.GetUserByOpenId(openid);
        if (user != null) {
            return user.getUserId();
        }
        throw new BusinessException("OpenId不存在:" + openid);
    }

    @Override
    public UserModel getUserByUserName(String userName) {
        AuthUser user = userService.getUserByUserName(userName);
        if (user == null) {
            throw new BusinessException("用户名不存在");
        }
        UserModel userModel = new UserModel();
        userModel.setUserId(user.getUserId());
        userModel.setUserName(user.getUserName());
        userModel.setOpenId(user.getOpenId());
        return userModel;
    }


    @Override
    public UserModel getUserByUserId(String userId) {
        AuthUser user = userService.getUserByUserId(userId);
        if (user == null) {
            throw new BusinessException("用户名不存在");
        }
        UserModel userModel = new UserModel();
        userModel.setUserId(user.getUserId());
        userModel.setUserName(user.getUserName());
        userModel.setOpenId(user.getOpenId());
        return userModel;
    }

    /**
     * 绑定第三方账号
     *
     * @param userId
     * @param thridSite
     */
    @Override
    public void BindThridSite(String userId, ThridSite thridSite) {

        AuthUserThrid thrid = new AuthUserThrid();
        thrid.setThridId(IdUtil.fastUUID());
        thrid.setThridType(thridSite.getThridType());
        LoginType loginType = LoginType.valueOf(thridSite.getThridType().toUpperCase());
        thrid.setThridName(loginType.getDesc());
        thrid.setUserId(userId);
        thrid.setThridUserId(thridSite.getUserId());
        thrid.setThridUserName(thridSite.getUserName());
        thrid.setThridNickName(thridSite.getNickName());
        thrid.setCreateTime(new Date());
        userThridService.SaveThrid(thrid);

    }

    @Override
    public void updateOpenId(String userId, String openId) {
        userService.UpdateOpenId(userId, openId);
    }


}
